package com.ljx.rpc.core.transport.socket.client;

import com.ljx.rpc.core.domain.dto.RpcRequest;
import com.ljx.rpc.core.exception.RpcException;
import com.ljx.rpc.core.transport.RpcClient;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-10-07  19:42
 */
public class SocketClient implements RpcClient {

    @Override
    public Object sendRequest(RpcRequest rpcRequest) {
        try {
            Socket socket = new Socket("127.0.0.1", 3600);
            ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
            ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
            outputStream.writeObject(rpcRequest);
            outputStream.flush();
            return inputStream.readObject();
        } catch (IOException e) {
            throw new RpcException(e.getMessage());
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
